package jam.network.message;

import jam.network.NetworkController;
import jam.model.ClientModel;
import jam.UserInterface.UI.UIController;
import java.util.Scanner;
import java.lang.String;

public class ResponseLogin extends ServerResponse {

	public static void process(String msg, ClientModel cm) {
		Scanner s = new Scanner( msg );
		s.useDelimiter(":");

		//skip keyword
		s.next();

		//get the username from the original message
		int id = s.nextInt();
		String orig = NetworkController.sent.get(id);
		Scanner msgScan = new Scanner(orig).useDelimiter(":");
		msgScan.next();
		msgScan.next();
		String user = msgScan.next();

		//Cleanup the sent hashmap
		NetworkController.sent.remove(id);

		String result = s.next();
		

		if ( result.equalsIgnoreCase("TRUE") ) {
			System.out.println(user  + "  logged in");

			ClientRequestUserInfo info = new ClientRequestUserInfo(user);
			NetworkController.send(info.toString());

			ClientRequestContactList cl = new ClientRequestContactList();
			UIController.createMainWindow(user.toString(), cm);
			
		}
		else {
			UIController.createLoginWindow();
			System.out.println("Login failed");
		}
	}

}